package com.parking.carsystem.parkingchargesystem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.common.Constant;
import com.parking.carsystem.parkingchargesystem.module.GroupListModule;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;

public class UpdateGroupActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.edittext_group_name)
    EditText edittextGroupName;
    @BindView(R.id.update_paking)
    TextView updatePaking;
    //点击的
    private int position;
    private GroupListModule.DataBean dataBean;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_update;
    }

    @Override
    public void initView() {
        text.setText("编辑分组名");
        dataBean = getIntent().getParcelableExtra(Constant.GROUP_NAME);
        position = getIntent().getIntExtra(Constant.GROUP_POSITION, -1);
        edittextGroupName.setText(dataBean.groupName);
        back.setVisibility(View.VISIBLE);
        updatePaking.setOnClickListener(this);
        back.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.update_paking:
                //更新分组名字
                String newGroupName = edittextGroupName.getText().toString().trim();
                if (!TextUtils.equals(dataBean.groupName, newGroupName)) {
                    updateGroup(newGroupName);
                } else {
                    showToast("请更改组名称");
                }
                break;
            case R.id.back:
                finish();
                break;
        }
    }

    private void updateGroup(final String newGroupName) {
        showLoading();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("id", dataBean.id);
        hashMap.put("groupName", newGroupName);
        JSONObject jsonObject = new JSONObject(hashMap);
        OkGo.<String>post(Constant.url + Constant.GROUPEDIT)
                .upJson(jsonObject.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        stopLoading();
                        try {
                            JSONObject jsonObject1 = new JSONObject(response.body());
                            int code = jsonObject1.getInt("code");
                            String msg = jsonObject1.getString("msg");
                            if (code == 200) {
                                Intent intent = new Intent();
                                intent.putExtra(Constant.GROUP_NAME, newGroupName);
                                intent.putExtra(Constant.GROUP_POSITION, position);
                                setResult(Constant.UPDATEGROUPNAME, intent);
                                finish();
                            } else {
                                showToast(msg);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        stopLoading();
                        showToast(response.message());
                    }
                });
    }
}
